package com.learn.practice.controller;

import cn.hutool.core.util.RandomUtil;
import com.learn.practice.config.KafkaConfiguration;
import com.learn.practice.pojo.LoginUser;
import com.learn.practice.security.AdminAuthenticationSuccessHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
public class IndexController {

//    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping("hello")
    public String hello(){
        return "hello world";
    }

    @GetMapping("home")
    public String home(){
        return "home";
    }

    @PreAuthorize(value = "hasAuthority('user/home2')")
    @GetMapping("home2")
    public String home2(){
        return "home2";
    }

    @GetMapping("/mobile/code")
    public String getMobileCode(String mobile){
        //产生四位随机数
        long rand = RandomUtil.randomLong(1000, 9999);
        //调用手机服务商接口
        AdminAuthenticationSuccessHandler.mobileCodeCache.put(mobile,rand);

        return String.valueOf(rand);
    }

    @PostMapping("logout1")
    public String logout(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (Objects.isNull(authentication)){
            throw new RuntimeException("登录失效，请重新登录");
        }
        LoginUser userDetails = (LoginUser) authentication.getPrincipal();
        if (Objects.isNull(userDetails)){
            throw new RuntimeException("用户失效，请重新登录");
        }

        AdminAuthenticationSuccessHandler.jwtTokenCache.remove(userDetails.getUsername());
        SecurityContextHolder.clearContext();
        return "logout";
    }

    public String getToken(){
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new RuntimeException("授权令牌为空");
        }
        return token;
    }


    /**
     * 发送消息
     * @param msg
     * @return
     */
    @RequestMapping("/send")
    public String sendMsg(@RequestParam("msg") String msg) {
        kafkaTemplate.send(KafkaConfiguration.SPRING_BOOT_TEST_TOPIC, "test-key", msg);
        return "已发送";
    }

    /**
     * 接收消息
     * @param msg
     */
//    @KafkaListener(topics = KafkaConfiguration.SPRING_BOOT_TEST_TOPIC)
    public void receiveMsg(String msg) {
        System.out.println("收到 Kafka 消息: " + msg);
    }
}
